package com.mccken.practice.top100.Top_1$20.top_4;

/**
 * 11. 盛最多水的容器
 * https://leetcode.cn/problems/container-with-most-water/?envType=study-plan-v2&envId=top-100-liked
 * 
 * 暴力解法超时
 * 需要 双指针  --->  学习到了双指针的好处，双指针什么条件下移动~，根据题干去分析，分析得当了再动手写代码
 *
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2023-12-29 22:31
 **/
public class MaxArea {

	// 双指针
	public int maxArea(int[] height) {
		if (height == null || height.length < 2) {
			return 0;
		}
		int max = 0;
		int i = 0;
		int j = height.length - 1;
		while (i < j) {
			max = Math.max(Math.min(height[i], height[j]) * (j - i), max);
			if (height[i] <= height[j]) {
				i++;
			} else {
				j--;
			}
		}
		return max;
	}

	// 暴力破解法，导致了超时
	public int maxArea1(int[] height) {

		if (height == null || height.length < 2) {
			return 0;
		}
		int max = 0;
		int j;
		int width = 1;
		while (width < height.length) {
			j = width;
			int i= 0;	
			while (j < height.length) {
				max = Math.max(Math.min(height[i], height[j]) * width, max);
				i++;
				j++;
			}

			width++;
		}
		return max;
	}

	public static void main(String[] args) {
		int[] height = {1,8,6,2,5,4,8,3,7};
		MaxArea maxArea =  new MaxArea();
		int max = maxArea.maxArea(height);
		System.out.println(max);
	}

}
